<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="客户编号" prop="clientCode">
        <el-input
          v-model="queryParams.clientCode"
          placeholder="请输入客户编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户名称" prop="clientName">
        <el-input
          v-model="queryParams.clientName"
          placeholder="请输入客户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户电话" prop="clientTel">
        <el-input
          v-model="queryParams.clientTel"
          placeholder="请输入客户电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="跟进人" prop="followName">
        <el-select
          v-model="queryParams.followName"
          placeholder="请选择跟进入姓名"
          :disabled="clientBelongDisabled"
        >
          <el-option
            v-for="item in clientBelongList"
            :key="item.userId"
            :label="item.label"
            :value="item.userName"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="跟进方式" prop="followMethod">
        <el-select v-model="queryParams.followMethod" placeholder="请选择跟进方式">
          <el-option
            v-for="dict in dict.type.follow_method"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="跟进日期" prop="followDate">
        <el-date-picker clearable
                        v-model="queryParams.followDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择跟进日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['mes:md:follow:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['mes:md:follow:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['mes:md:follow:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['mes:md:follow:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="followList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="客户编号" align="center" prop="clientCode"/>
      <el-table-column label="客户名称" align="center" prop="clientName"/>
      <el-table-column label="客户电话" align="center" prop="clientTel"/>
      <el-table-column label="跟进人姓名" align="center" prop="followName"/>
      <el-table-column label="跟进方式" align="center" prop="followMethod">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.follow_method"
            :value="scope.row.followMethod"
          />
        </template>
      </el-table-column>
      <el-table-column label="跟进日期" align="center" prop="followDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.followDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="跟进结果" align="center" prop="followResult"/>
      <el-table-column label="跟进信息" align="center" prop="followMsg"/>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['md:follow:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['md:follow:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改客户跟进信息对话框 -->
    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="960px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="客户名称" prop="clientName">
              <el-select
                v-model="form.clientName"
                placeholder="请选择客户名称"
                :change="selectClientCode(form.clientName)"
              >
                <el-option
                  v-for="item in this.clientList"
                  :key="item.clientCode"
                  :label="item.label"
                  :value="item.clientName"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="客户编码" prop="clientCode">
              <el-input v-model="form.clientCode" placeholder="请输入客户编码" :disabled="clientDisabled"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="客户电话" prop="clientTel">
              <el-input v-model="form.clientTel" placeholder="请输入客户电话" :disabled="clientDisabled"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="跟进人" prop="followName">
              <el-select
                v-model="form.followName"
                placeholder="请选择跟进入姓名"
                :disabled="clientBelongDisabled"
              >
                <el-option
                  v-for="item in clientBelongList"
                  :key="item.userId"
                  :label="item.label"
                  :value="item.userName"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="跟进方式" prop="followMethod">
              <el-select v-model="form.followMethod" placeholder="请选择跟进方式">
                <el-option
                  v-for="dict in dict.type.follow_method"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="跟进日期" prop="followDate">
              <el-date-picker clearable
                              v-model="form.followDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择跟进日期"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="跟进结果" prop="followResult">
              <el-input v-model="form.followResult" placeholder="请输入跟进结果"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="跟进信息" prop="followMsg">
              <el-input v-model="form.followMsg" placeholder="请输入跟进信息"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" placeholder="请输入备注"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFollow, getFollow, delFollow, addFollow, updateFollow } from '@/api/mes/md/follow'
import { listClient } from '@/api/mes/md/client'
import { listUser } from '@/api/system/user'

export default {
  name: 'Follow',
  dicts: ['follow_method'],
  props: {
    followList: {
      type: Object,
      default: ''
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户跟进信息表格数据
      followList: [],
      //客户信息数据
      clientList: [],
      clientBelongList: [],
      clientDisabled: false,
      clientBelongDisabled: false,
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        clientCode: null,
        clientName: null,
        clientTel: null,
        followName: null,
        followMethod: null,
        followDate: null,
        followResult: null,
        followMsg: null
      },
      queryClientParams: {
        pageNum: 1,
        pageSize: 100
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      queryUserParams: {
        deptId: 123,
        pageNum: 1,
        pageSize: 10
      }
    }
  },
  created() {
    this.getList()
    this.getClientList()
    this.getClientBelongList()
  },
  methods: {
    /** 查询客户跟进信息列表 */
    getList() {
      this.loading = true
      listFollow(this.queryParams).then(response => {
        this.followList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    getClientList() {
      listClient(this.queryClientParams).then(response => {
        this.clientList = response.rows
      })
    },
    getClientBelongList() {
      listUser(this.queryUserParams).then((response) => {
        this.clientBelongList = response.rows
      })
    },
    selectClientCode(clientName) {
      this.clientList.forEach(item => {
        if (item.clientName == clientName) {
          this.form.clientName = item.clientName
          this.form.clientCode = item.clientCode
          this.form.clientTel = item.tel
          this.form.followName = item.clientBelong
          this.clientDisabled = true
          if (!this.$store.getters.roles == 'SalesManager' || !this.$store.getters.roles == 'admin') {
            this.clientBelongDisabled = true
          }
        }
      })

    },
    // 取消按钮
    cancel() {
      this.clientDisabled = false
      this.clientBelongDisabled = false
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.clientDisabled = false
      this.clientBelongDisabled = false
      this.form = {
        id: null,
        clientCode: null,
        clientName: null,
        clientTel: null,
        followName: null,
        followMethod: null,
        followDate: null,
        followResult: null,
        followMsg: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加客户跟进信息'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getFollow(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改客户跟进信息'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.clientDisabled = false
      this.clientBelongDisabled = false
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateFollow(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addFollow(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除客户跟进信息编号为"' + ids + '"的数据项？').then(function() {
        return delFollow(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('mes/md/follow/export', {
        ...this.queryParams
      }, `follow_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
